// this code verified on Chrome 18.0.1025.162 

/*
 * GLOBAL VARS
 */
var votingURL = "http://97xonline.upickem.net/engine/ApprovedSubmissions.aspx?PageType=APPROVED&contestid=57027"
var votingPosition = 3;

/*
 * FUNCIONS
 */

function addNotifyMsg(msg) {
  notifyDiv.innerText += msg + "\n";
}

function votingFrameLoad() {
  addNotifyMsg("voting iframe has finished loading");
  // search for voting radio button
  var votingFrameDocument = (votingFrame.contentDocument || votingFrame.contentWindow);
  if (votingFrameDocument.document) {
    votingFrameDocument = votingFrameDocument.document;
  }
  
  var radioButtons = votingFrameDocument.getElementsByName("SubmissionVote0");
  
  if (radioButtons.length == 0) {
    addNotifyMsg("did not find voting radio buttons.  giving up for now.");
    return;
  }

  // make sure this function does not fire after the page loads from vote submission
  votingFrame.removeAttribute("onload");
  
  radioButtons.item(votingPosition).checked = "true";
  votingFrameDocument.getElementById('HiddenFormSubmitted').value = "VOTE";
  votingFrameDocument.getElementById('PageForm').submit();
  
  addNotifyMsg("submitted vote.");
}


/*
 * MAIN CODE
 */
 
// create notification div
var notifyDiv = document.createElement("div");
notifyDiv.setAttribute("id", "notifyDiv");
notifyDiv.style.position = "absolute";
notifyDiv.style.top = "0";
notifyDiv.style.left = "0";
notifyDiv.style.width = "80em";
notifyDiv.style.height = "24em";
notifyDiv.style.zIndex = "99999";
notifyDiv.style.backgroundColor = "#000000";
notifyDiv.style.color = "#BBBBBB";
notifyDiv.style.overflow = "auto";
document.body.appendChild(notifyDiv);

addNotifyMsg("loaded voter");

// create iframe for voting page
var votingFrame = document.createElement("iframe");
votingFrame.setAttribute("id", "votingFrame");
votingFrame.setAttribute("onload", "votingFrameLoad()");
document.body.appendChild(votingFrame);
votingFrame.setAttribute("src", votingURL);